/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_rtsp_detect.c                                    */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/04 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include "../../../src/module/rtsp/config.h"
#include "../test_rtsp_comm.h"
#include <rtsp/rtsp_core.h>
#include <rtsp/rtsp_header.h>
#include <rtsp/rtsp_comm.h>
#include <rtsp/rtsp_packet.h>
#include <frct_comm.h>
#include <apmtest_comm.h>

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_rtsp_create_ct(&p_st_conn, TEST_RTSP_SRC_IP, TEST_RTSP_DST_IP, TEST_RTSP_SRC_PORT, TEST_RTSP_DST_PORT), FR_SUCC);

    char *p_payload = (char *) "OPTIONS rtsp://192.168.24.59:554 RTSP/1.0\r\n"
            "CSeq: 2\r\n"
            "User-Agent: LibVLC/2.2.1 (LIVE555 Streaming Media v2014.07.25)\r\n\r\n";
    unsigned int ui_payload_len = strlen(p_payload);
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, p_payload, ui_payload_len, TEST_RTSP_SRC_IP, TEST_RTSP_DST_IP, TEST_RTSP_SRC_PORT, TEST_RTSP_DST_PORT, st_timestamp), FR_SUCC);

    CU_ASSERT_EQUAL(rtsp_detect(p_st_conn, p_st_packet), APM_L7_IS_YES);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
    apmtest_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

static void __case2(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_rtsp_create_ct(&p_st_conn, TEST_RTSP_SRC_IP, TEST_RTSP_DST_IP, TEST_RTSP_SRC_PORT, TEST_RTSP_DST_PORT), FR_SUCC);

    char *p_payload = (char *) "RTSP/1.0 200 OK\r\n"
            "CSeq: 92\r\n"
            "Transport: RTP/AVP;multicast;destination=224.0.1.12;\r\n"
            "           port=61010-61011;mode=record;ttl=127\r\n\r\n";
    unsigned int ui_payload_len = strlen(p_payload);
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, p_payload, ui_payload_len, TEST_RTSP_SRC_IP, TEST_RTSP_DST_IP, TEST_RTSP_SRC_PORT, TEST_RTSP_DST_PORT, st_timestamp), FR_SUCC);

    CU_ASSERT_EQUAL(rtsp_detect(p_st_conn, p_st_packet), APM_L7_IS_YES);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
    apmtest_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

static void __case3(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_rtsp_create_ct(&p_st_conn, TEST_RTSP_SRC_IP, TEST_RTSP_DST_IP, TEST_RTSP_SRC_PORT, TEST_RTSP_DST_PORT), FR_SUCC);

    char *p_payload = (char *) "RTS/1.0 200 OK\r\n"
            "CSeq: 92\r\n"
            "Transport: RTP/AVP;multicast;destination=224.0.1.12;\r\n"
            "           port=61010-61011;mode=record;ttl=127\r\n\r\n";
    unsigned int ui_payload_len = strlen(p_payload);
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, p_payload, ui_payload_len, TEST_RTSP_SRC_IP, TEST_RTSP_DST_IP, TEST_RTSP_SRC_PORT, TEST_RTSP_DST_PORT, st_timestamp), FR_SUCC);

    CU_ASSERT_EQUAL(rtsp_detect(p_st_conn, p_st_packet), APM_L7_IS_UNKNOWN);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
    apmtest_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite_rtsp_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" rtsp_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
